package me.leiusual.sharing.ui.shop.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import me.leiusual.sharing.R
import me.leiusual.sharing.api.model.Withdraw

/**
 * author: leiusual
 * create on: 2019-08-01
 * description:
 */
class ShopWithdrawAdapter(
    private val applyListener: (Withdraw) -> Unit,
    private val cancelListener: (Withdraw) -> Unit
) : PagedListAdapter<Withdraw, ShopWithdrawViewHolder>(DIFF_CALLBACK) {

  companion object {
    private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Withdraw>() {

      override fun areItemsTheSame(oldItem: Withdraw, newItem: Withdraw): Boolean {
        return oldItem.id == newItem.id
      }

      override fun areContentsTheSame(oldItem: Withdraw, newItem: Withdraw): Boolean {
        return oldItem == newItem
      }
    }
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShopWithdrawViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val itemView = inflater.inflate(R.layout.view_holder_shop_withdraw, parent, false)
    return ShopWithdrawViewHolder(itemView, applyListener, cancelListener)
  }

  override fun onBindViewHolder(holder: ShopWithdrawViewHolder, position: Int) {
    holder.bind(getItem(position) ?: return)
  }
}